/*
 * @Description: 
 * @Author: liyuhao
 * @Date: 2021-08-16 17:56:14
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-11-15 16:30:34
 */
import { ImgTypeEnum } from '@/enums/global/enum'

interface SubmitImgData {
    logo?: any[]
    wechatQR?: any[]
    weiboQR?: any[]
    imgList?: any[]
}

interface DetailImgList {
    id?: number | null
    relateFrom?: number
    fileName?: string
    filePath?: string
    size?: number
}

//格式化图片数据
const formatImg = (data: any) => {
    if (!data) return
    const { fileName, filePath, id, size } = data || {}
    return {
        type: fileName?.split('.').pop(),
        uid: id,
        thumbUrl: filePath,
        url: filePath,
        name: fileName,
        size: size,
        id
    }
}

// 转换提交时的图片数据
export const formatSubmitImg = (data: SubmitImgData) => {
    const { logo, wechatQR, weiboQR, imgList } = data || {}
    const [_logo] = logo || []
    const [_wechatQR] = wechatQR || []
    const [_weiboQR] = weiboQR || []
    const elseImgs = (imgList || []).map((v: any) => ({
        relateFrom: ImgTypeEnum.Else,
        fileName: v?.name,
        filePath: v?.url,
        size: v?.size
    }))

    const res = []
    _logo && res.push({
        relateFrom: ImgTypeEnum.Logo,
        fileName: _logo?.name,
        filePath: _logo?.url,
        size: _logo?.size
    })
    _wechatQR && res.push({
        relateFrom: ImgTypeEnum.Wechat,
        fileName: _wechatQR?.name,
        filePath: _wechatQR?.url,
        size: _wechatQR?.size
    })
    _weiboQR && res.push({
        relateFrom: ImgTypeEnum.Weibo,
        fileName: _weiboQR?.name,
        filePath: _weiboQR?.url,
        size: _weiboQR?.size
    })

    return [
        ...res,
        ...elseImgs
    ]
}

// 转换详情里的图片数据
export const formatDetailImg = (list: DetailImgList[]) => {
    const imgList: any[] = [];
    let logo, wechatQR, weiboQR;
    (list || []).forEach((v: any) => {
        switch (v?.relateFrom) {
            case ImgTypeEnum.Logo:
                logo = [formatImg(v)]
                break
            case ImgTypeEnum.Wechat:
                wechatQR = [formatImg(v)]
                break
            case ImgTypeEnum.Weibo:
                weiboQR = [formatImg(v)]
                break
            case ImgTypeEnum.Else:
                imgList.push(formatImg(v))
                break
            default:
                break
        }
    })
    return {
        logo,
        wechatQR,
        weiboQR,
        imgList
    }
}

/* ===== 分页相关格式化 start ===== */
export interface Pagination {
    current?: number
    pageSize?: number
}

// 所有使用useRequest的分页接口都需要调用此函数处理入参数
export const formatPaginatedParams = (params?: any, pageKey = 'current', sizeKey = 'size', startPage = 1) => {
    // 暂不支持多列同时排序
    const { current, pageSize, filters, sorter, ...otherParams } = params || {}
    const page = typeof current === 'number' && !isNaN(current) && current > 0 ? current - (1 - startPage) : 0
    return {
        ...otherParams,
        [pageKey]: page,
        [sizeKey]: pageSize,
    }
}

//对结果进行格式化，适配usePagination
export const formatPaginatedResult = (res?: any, listKey = 'records', totalKey = 'total') => {
    const { [listKey]: _list, [totalKey]: _total, ...otherRes } = res || {}
    const list = Object.prototype.toString.call(_list).slice(8, -1) === 'Array' ? _list : []
    const total = typeof _total === 'number' && !isNaN(_total) ? _total : 0

    return {
        ...otherRes,
        list,
        total
    }
}
/* ===== 分页相关格式化 end ===== */

//对区域数据格式化
export const formatRegions = (regions: any[]) => {
    return regions.map(item => {
        const option: any = { label: item.areaName, value: item.id };
        if (item.children?.length) {
            option.children = formatRegions(item.children);
        }
        return option;
    })
}

/* ===== chart相关格式化 start ===== */
//对pie legend format
export const pieLegendFormat = (name: string, pieData: any[] = [], total?: number) => {
    const totalValue = total || pieData.reduce((previous, current) =>
        previous.value + current.value, { value: 0 }
    )
    const filterData = pieData.filter((item: any) => name === item.name);
    const valueStr = filterData.length > 0
        ? ((filterData[0].value / totalValue) * 100).toFixed(2)
        : '0';
    const result = name + ':\t' + valueStr + '%'
    if (result.length > 14) {
        const midIndex = Math.floor(result.length / 2) - 2;
        return result.substring(0, midIndex) + '\n' + result.substring(midIndex)
    }
    return result;
}


// 扁平树转换
export const formatTree = (tree: any[]) => {
    const map: any = {},
        res: any[] = []
    for (const item of tree) {
        map[item.id] = item
        item.children = []
        item.title = item.menuName
        item.key = item.id
    }
    for (const item of tree) {
        if (map[item.pid]) {
            map[item.pid].children.push(item)
        } else {
            res.push(item)
        }
    }
    return res
}